package net.yc.imalachat.push.activities;

import android.app.SearchManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import androidx.appcompat.widget.SearchView;

import com.example.common.app.Fragment;
import com.example.common.app.ToolBarActivity;

import net.yc.imalachat.push.R;
import net.yc.imalachat.push.frags.search.SearchGroupFragment;
import net.yc.imalachat.push.frags.search.SearchUserFragment;

public class SearchActivity extends ToolBarActivity {
    private static final String TAG = "SearchActivity";

    private static final String EXTRA_TYPE = "EXTRA_TYPE";
    public static final int TYPE_USER = 1;  // 搜索用户
    public static final int TYPE_GROUP = 1; // 搜索群

    private int type;
    private SearchFragment mSearchFragment;
    /**
     * 显示搜索的界面
     * @param mContext 上下文
     * @param type      显示的类型，用户还是群体
     */
    public static void showSearch(Context mContext, int type) {
        Intent intent = new Intent(mContext, SearchActivity.class);
        intent.putExtra(EXTRA_TYPE, type);
        mContext.startActivity(intent);
    }

    @Override
    protected boolean initArgs(Bundle bundle) {
        type = bundle.getInt(EXTRA_TYPE); 
        return type == TYPE_USER || type == TYPE_GROUP;
    }

    @Override
    protected void initWidget() {
        super.initWidget();
        // 显示对应的Fragment
        Fragment fragment;
        if (type == TYPE_USER) {
            SearchUserFragment userFragment = new SearchUserFragment();
            fragment = userFragment;
            mSearchFragment = userFragment;
        } else {
            SearchGroupFragment groupFragment = new SearchGroupFragment();
            fragment = groupFragment;
            mSearchFragment = groupFragment;
        }

        getSupportFragmentManager().beginTransaction()
                .add(R.id.layout_container, fragment)
                .commit();
    }

    @Override
    protected int getContentLayoutId() {
        return R.layout.activity_search;
    }

    /**
     * 初始化菜单
     * @param menu
     * @return
     */
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // 初始化菜单
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.search, menu);
        // 找到我们的搜索菜单
        MenuItem searchItem = menu.findItem(R.id.action_search);
        // 这俄格SearchView是因为我们在action_search.xml中定义了这个
        // app:actionViewClass="androidx.appcompat.widget.SearchView
        SearchView searchView = (SearchView) searchItem.getActionView();

        /**
         * 这种默认写法
         */
        if (searchView != null) {

            // 拿到搜索的管理器
            SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
            searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
            // 添加搜索监听
            searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
                @Override
                public boolean onQueryTextSubmit(String query) {
                    // 当点击了提交按钮的时候
                    search(query);
                    return true;
                }

                @Override
                public boolean onQueryTextChange(String newText) {
                    // 当我们的字体改变的时候，我们不会及时搜索，只会在为null的情况进行搜索
                    if (TextUtils.isEmpty(newText)) {
                        search("");
                        return true;
                    }

                    return false;
                }
            });
        }
        return super.onCreateOptionsMenu(menu);
    }

    /**
     * 搜索的发起点
     */
    private void search(String str) {
        if (mSearchFragment == null) {
            return ;
        }
        mSearchFragment.search(str);
    }

    /**
     * 搜索的Fragment必须继承的接口
     */
    public interface SearchFragment {
        void search(String content);
    }
}